<template>
  <div>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="name" label="" width="50">
        <template>
          <span class="el-icon-bottom"></span>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="180"> </el-table-column>
      <el-table-column prop="deptType" label="审核类型" width="100">
        <template slot-scope="{ row }">
          <span v-if="row.deptType == 'R'">角色审核</span>
          <span v-else-if="row.deptType == 'U'">人员审核</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="auditorList"
        label="审核角色"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ auditNames(scope.row) }}
        </template>
      </el-table-column>
      <el-table-column prop="des" label="描述" width="150">
        <template slot-scope="scope">
          <p class="sheng">{{ scope.row.des || '--' }}</p>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="140">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button
              type="primary"
              size="mini"
              @click="handleEdit(row)"
              :disabled="row.step < 0"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="handleDelete(row)"
              :disabled="row.step < 0"
              >删除</el-button
            >
          </el-button-group>
          <!-- :disabled="flowData.status == 1" -->
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
import {
  flowDefinitionNodeQuery,
  flowDefinitionNodeDelete,
} from '@/api/admin/audit/flowDefinitionNode.js'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        definitionCode: '',
      },
      tableData: [],
      pageObj: {},
    }
  },
  computed: {
    auditNames() {
      return function (v) {
        let { deptType, auditorList, auditRoles } = v
        if (deptType == 'R') {
          if (auditRoles && auditRoles.length) {
            return auditRoles.map((v) => v.name).toString() || '--'
          }
        } else {
          if (auditorList && auditorList.length) {
            let arr = auditorList.filter((m) => m != null)
            return arr.map((m) => m.userName).toString() || '--'
          }
        }
        return '--'
      }
    },
  },
  props: ['flowData'],
  mounted() {
    this.getList()
    Bus.$on('refreshFlowNodeList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    Bus.$off('refreshFlowNodeList')
  },
  methods: {
    getList() {
      this.listQuery.definitionCode = this.flowData.code
      flowDefinitionNodeQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data
        this.pageObj = res.data
      })
    },
    //编辑
    handleEdit(row) {
      Bus.$emit('showDialogFlowNodeAdd', row)
    },
    //删除确认
    handleDelete(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.delete(row)
        })
        .catch(() => {})
    },
    //删除
    delete(row) {
      flowDefinitionNodeDelete(row.id).then((res) => {
        if (!res || res.data.code != 0) return
        this.getList()
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
  },
}
</script>